/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiBuscarUsuarioRelatorio.java
 *
 * Created on 29/11/2011, 00:10:52
 */
package view.gui;

import controller.ControllerRelatorio;
import java.util.List;
import javax.swing.JOptionPane;
import model.dao.DaoUsuario;
import model.dominio.Usuario;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Rodrigo
 */
public class GuiBuscarUsuarioRelatorio extends JInternalFramePermissao {

    /** Creates new form GuiBuscarUsuarioRelatorio */
    public GuiBuscarUsuarioRelatorio() {
        initComponents();
        DaoUsuario dao = new DaoUsuario();
        List<Usuario> l = dao.buscarUsuarios();
        for (Usuario u : l) {
            usuario.addItem(u);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        localizarCliente = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        dataFinal = new javax.swing.JFormattedTextField();
        dataInicial = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        usuario = new javax.swing.JComboBox();

        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros de busca"));
        jPanel1.setLayout(null);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(" a ");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(190, 80, 30, 30);

        localizarCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/imagens/Search-icon.png"))); // NOI18N
        localizarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                localizarClienteActionPerformed(evt);
            }
        });
        jPanel1.add(localizarCliente);
        localizarCliente.setBounds(350, 20, 40, 40);

        jLabel2.setText("Período:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(20, 80, 60, 30);

        try {
            dataFinal.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jPanel1.add(dataFinal);
        dataFinal.setBounds(220, 80, 110, 30);

        try {
            dataInicial.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jPanel1.add(dataInicial);
        dataInicial.setBounds(80, 80, 110, 30);

        jLabel3.setText("Usuário:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(20, 30, 60, 30);

        jPanel1.add(usuario);
        usuario.setBounds(80, 30, 250, 30);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 420, 130);

        setBounds(0, 0, 436, 159);
    }// </editor-fold>//GEN-END:initComponents

    private void localizarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_localizarClienteActionPerformed
        if (usuario.getSelectedItem() instanceof Usuario) {
            JasperPrint relat = ControllerRelatorio.relatorioLog(dataInicial.getText(), dataFinal.getText(),(Usuario) usuario.getSelectedItem());
            if (!relat.getPages().isEmpty()) {
                JasperViewer.viewReport(relat, false);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(null, "Não foram encontrados dados para esse periodo.", "Falha.", JOptionPane.ERROR_MESSAGE);
            }
        }
}//GEN-LAST:event_localizarClienteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField dataFinal;
    private javax.swing.JFormattedTextField dataInicial;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton localizarCliente;
    private javax.swing.JComboBox usuario;
    // End of variables declaration//GEN-END:variables

    @Override
    int getPermissao() {
        return 26;
    }
}
